package vn.com.shoppie.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.http.client.ClientProtocolException;

import vn.com.shoppie.network.WiModelManager;
import vn.com.shoppie.object.GiftAdapter;
import vn.com.shoppie.object.HeaderGiftsAdapter;
import vn.com.shoppie.setting.SettingPreference;
import vn.com.shoppie.sobject.Gift;
import vn.com.shoppie.sobject.ShoppieObject;
import vn.com.shoppie.sobject.Slider;
import vn.com.shoppie.util.SConfig;
import vn.com.shoppie.util.SUtilXml;
import vn.com.shoppie.view.CirclePageIndicator;
import vn.com.shoppie.view.PageIndicator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class GiftActivity extends FragmentActivity {

	HeaderGift mHeaderShops;
	ListView mShops;
	private ArrayList<Gift> mShopData;
	private GiftAdapter mAdapterGift;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_shops);

		mShops = (ListView) findViewById(R.id.lv_shops);
		mHeaderShops = new HeaderGift(this);
		mShopData = new ArrayList<Gift>();
		mAdapterGift = new GiftAdapter(this, mShopData);
		mShops.addHeaderView(mHeaderShops);
		mShops.setAdapter(mAdapterGift);
		
		
	}

	public void btnClicked(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			finish();
			break;

		default:
			break;
		}
	}
	@Override
	protected void onResume() {
		super.onResume();
		refreshUI();
	}
	private void refreshUI(){
		// load list gifts
		String url=SConfig.URL_SHOPPIE_HOME+SConfig.URL_GIFTS;
		new AsyncTask<String, Integer, String>() {
			WiModelManager mng = new WiModelManager();

			@Override
			protected String doInBackground(String... arg0) {
				String xml = null;
				try {

					xml = SUtilXml.getInstance().getXmlFromLink(GiftActivity.this, arg0[0]);
					mng.parse(xml);
				} catch (ClientProtocolException e) {
					if(SettingPreference.PRINT_STACK_TRACE){
						e.printStackTrace();
					}
					System.out.println(e.toString());
				} catch (IOException e) {
					if(SettingPreference.PRINT_STACK_TRACE){
						e.printStackTrace();
					}
					System.out.println(e.toString());
				}
				
				return xml;
			}

			protected void onPostExecute(String xml) {
				mShopData.clear();
				ArrayList<ShoppieObject> lstTmp = mng.getResultShoppieObject();
				Log.e("Gift", lstTmp.size()+"");
				for(ShoppieObject item: lstTmp){
						mShopData.add((Gift)item);
				}
				GiftActivity.this.mAdapterGift.notifyDataSetChanged();
			};
		}.execute(url);
	}
	@SuppressLint("NewApi")
	public class HeaderGift extends RelativeLayout {
		HeaderGiftsAdapter mAdapter;
		ViewPager mPager;
		PageIndicator mIndicator;
		Timer timer = new Timer();
		ArrayList<Slider> slides=new ArrayList<Slider>();
		public HeaderGift(Context context) {
			super(context);
			LayoutInflater li = (LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
			li.inflate(R.layout.header_shops, this, true);
			mAdapter = new HeaderGiftsAdapter(getSupportFragmentManager(),slides);
			mPager = (ViewPager) findViewById(R.id.pager);
			mPager.setAdapter(mAdapter);
			mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
			mIndicator.setViewPager(mPager);
			// We set this on the indicator, NOT the pager
			mIndicator .setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
						@Override
						public void onPageSelected(int position) {

						}

						@Override
						public void onPageScrolled(int position,
								float positionOffset, int positionOffsetPixels) {
						}

						@Override
						public void onPageScrollStateChanged(int state) {

						}
					});
			timer.scheduleAtFixedRate(new TimerTask() {

				@Override
				public void run() {
					HeaderGift.this.refreshUI();
				}
			}, 0, 5000);
		}

		public void refreshUI() {
			// load Slider
			String url=SConfig.URL_SHOPPIE_HOME+SConfig.URL_GIFT_SLIDER;
			new AsyncTask<String, Void, Boolean>(){
				@Override
				protected Boolean doInBackground(String... arg0) {
					String url=arg0[0];
					try {
						String xml=SUtilXml.getInstance().getXmlFromLink(getApplicationContext(), url);
						WiModelManager mng=new WiModelManager();
						mng.parse(xml);
						ArrayList<ShoppieObject> data=mng.getResultShoppieObject();
						slides.clear();
						for(ShoppieObject item: data){
							slides.add((Slider)item);
						}
						
					} catch (ClientProtocolException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
					
					return false;
				}
				protected void onPostExecute(Boolean result) {
					try{
						mAdapter.notifyDataSetChanged();
					}catch(NullPointerException e){}
				}
			}.execute(url);
			
			// update ui
			mPager.post(new Runnable() {
				
				@Override
				public void run() {
					try {
						int crt = mPager.getCurrentItem();
						int size = mPager.getChildCount();
						if (crt < size)
							crt++;
						else
							crt = 0;
						if (mPager != null)
							mPager.setCurrentItem(crt, true);
					}catch(NullPointerException e){
						timer.cancel();
					}
					catch (IllegalStateException ex) {
						timer.cancel();
					}
				}
			});
		}
	}

}
